/**
  *@File文件:ShoppingCartController.java
  *@Date日期:2017年7月18日 上午9:59:04
  *@Copyright版权:Corporation 2017
  */
package com.qiaomengapp.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qiaomengapp.bean.GoodDetail;
import com.qiaomengapp.bean.User;
import com.qiaomengapp.bean.UserAddress;
import com.qiaomengapp.service.IShoppingCartService;
import com.qiaomengapp.service.IUserAddressService;

/**
 * @Description描述:
 * @Author作者 wangzongwen
 * @Date日期:2017年7月18日
 */
@Controller
@RequestMapping("shopping-cart/api")
public class ShoppingCartController {

	@Autowired
	private IShoppingCartService iShopBillService;

	@Autowired
	private IUserAddressService iUserAddressService;

	@RequestMapping("/shoppingCartGood")
	@ResponseBody
	public Object shoppingCartGood(HttpSession session) {
		User user = (User) session.getAttribute("user");
		if (user == null) {
			user = new User();
			user.setId(1);
		}
		// 得到购物车商品信息
		Map<String, List<GoodDetail>> map = getShoppingCartGoodMap(user.getId(), null);
		return map;
	}

	/**
	 * @Description描述: 查询购买的商品信息
	 * @param shopGoodIds
	 *            购买商品的id数组
	 * @return
	 * @return Object
	 */
	@RequestMapping("/showBuyGoodMessage")
	@ResponseBody
	public Object showBuyGoodMessage(Integer[] shopGoodIds, HttpSession session) {
		// 得到用户id
		User user = (User) session.getAttribute("user");
		if (user == null) {
			user = new User();
			user.setId(1);
		}
		// 根据商品id和用户id查询商品信息
		Map<String, List<GoodDetail>> map = getShoppingCartGoodMap(user.getId(), shopGoodIds);
		return map;
	}

	private Map<String, List<GoodDetail>> getShoppingCartGoodMap(int userId, Integer[] shopGoodIds) {
		// 查询购物车商品
		List<GoodDetail> shoppingCartGoods = iShopBillService.selectShoppingCartGood(userId, shopGoodIds);
		// 创建map集合保存信息
		Map<String, List<GoodDetail>> map = new HashMap<String, List<GoodDetail>>();
		// 遍历集合进行信息保存
		for (GoodDetail shoppingCartGood : shoppingCartGoods) {
			if (map.containsKey(shoppingCartGood.getShopName())) {
				List<GoodDetail> list = map.get(shoppingCartGood.getShopName());
				list.add(shoppingCartGood);
			} else {
				// 添加数据
				List<GoodDetail> list = new ArrayList<GoodDetail>();
				list.add(shoppingCartGood);
				map.put(shoppingCartGood.getShopName(), list);
			}
		}
		return map;
	}

	
	/**
	 *@Description描述: 得到用户的默认
	 *@param session
	 *@return
	 *@return UserAddress
	 */
	@RequestMapping("/selectUserAddressById")
	@ResponseBody
	public UserAddress selectUserAddressById(HttpSession session) {
		//得到session中的用户对象
		User user=(User) session.getAttribute("user");
		if (user==null) {
			user = new User();
			user.setDefaultAddressId(2);
		}
		UserAddress userAddress = iUserAddressService.selectUserAddressByid(user.getDefaultAddressId());
		return userAddress;
	}
}
